package linearstructure.stack.arraystack;

/**
 * @ author : HyoJoo-W
 * @ createDate : 2021-05-27 20:26
 * @ Description :
 */
public class ArrayStack
{
    private int maxSize;
    private int[] stack;
    private int top = -1;

    public ArrayStack(int maxSize)
    {
        this.maxSize = maxSize;
        stack = new int[this.maxSize];
    }

    public boolean isFull()
    {
        return top == maxSize - 1;
    }

    public boolean isEmpty()
    {
        return top == -1;
    }

    public int getTop()
    {
        return stack[top];
    }

    public void push(int value)
    {
        if (isFull())
        {
            System.out.println("栈已满");
            return;
        }
        top++;
        stack[top] = value;
    }

    public int pop()
    {
        if (isEmpty())
        {
            throw new RuntimeException("栈空");
        }
        int value = stack[top];
        top--;
        return value;
    }

    public void list()
    {
        if (isEmpty())
        {
            System.out.println("栈空");
            return;
        }
        for (int i = maxSize - 1; i >= 0; --i)
        {
            System.out.printf("stack[%d] = %d \n", i, stack[i]);
        }
    }

    //返回运算符的优先级
    public int getPriority(int opr)
    {
        if (opr == '*' || opr == '/')
        {
            return 1;
        }
        else if (opr == '+' || opr == '-')
        {
            return 0;
        }
        else
        {
            return -1;
        }
    }

    //判读是不是一个运算符
    public boolean isOpr(char val)
    {
        return val == '*' || val == '/' || val == '+' || val == '-';
    }

    //二元计算
    public int cal(int opd1, int opd2, int opr)
    {
        int res = 0;
        switch (opr)
        {
            case '+':
                res = opd2 + opd1;
                break;
            case '-':
                res = opd2 - opd1;
                break;
            case '*':
                res = opd2 * opd1;
                break;
            case '/':
                res = opd2 / opd1;
                break;
        }
        return res;
    }


}
